﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Enities;
using BLToolkit.Data;
using System.Collections;
using DAL;
using System.Linq.Expressions;
using BLL;
using System.Data.SqlClient;


namespace QLThuVien_Nhom21
{
    public partial class QuanLyTraTaiLieu : Form
    {
        private bool _chon;
        public List<Exception> Exceptions { get; set; }
        private IGeneralRepository<TraTaiLieu> _repository1;
        public QuanLyTraTaiLieu()
        {
            InitializeComponent();
        }

        LuuTraTaiLieu trtl = new LuuTraTaiLieu();
        private void btnBaoCaoViPham_Click(object sender, EventArgs e)
        {
            BaoCaoViPham bcvp = new BaoCaoViPham();
            bcvp.Show();
        }

        // Danh Sach mượn tài liệu
        public void DsTraTaiLieu()
        {
            ArrayList list = new ArrayList();
            TraTaiLieu ttl;
            using (DbManager db = new DbManager())
            {
                db.SetCommand("SELECT * FROM TraTaiLieu");

                using (IDataReader rd = db.ExecuteReader())
                {
                    while (rd.Read())
                    {
                        // ...
                        ttl = new TraTaiLieu();
                        //ttl.MaDocGia =rd.GetType();
                        ttl.MaTaiLieu = rd.GetString(1);
                        ttl.NgayMuon = rd.GetDateTime(2);
                        ttl.NgayThucTra = rd.GetDateTime(3);


                        //pn.NgayLap =ConvertType.ToDateTime(rd.GetDateTime(2));

                        list.Add(ttl);
                    }
                    dtgvTraTL.DataSource = list;
                }
            }

        }



        private void btndanhSach_Click(object sender, EventArgs e)
        {
            _repository1 = new GeneralRepository<TraTaiLieu>();
            dtgvTraTL.DataSource = _repository1.All().ToList();
        }
        private void dtgvTraTL_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 0 && dtgvTraTL.Rows.Count > 0)
            {
                _chon = !_chon;
                for (int idx = 0; idx < dtgvTraTL.Rows.Count; idx++)
                {
                    dtgvTraTL.Rows[idx].Selected = _chon;
                    dtgvTraTL.Rows[idx].Cells[0].Value = _chon;
                }
            }
        }

        // Them Tra tai lieu
        private void btnthem_Click(object sender, EventArgs e)
        {
            int kq = 0;
            if (txtMaDocGia.Text.Length != 7)
            {
                MessageBox.Show("Ma doc gia phai co chieu dai 7 ky tu", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }


            try
            {
                //if (kiemtratontai())
                //{
                //    MessageBox.Show("Độc giả ko tồn tại !");
                //}
               
                    TraTaiLieu ttl = new TraTaiLieu();
                    ttl.MaDocGia = int.Parse(txtMaDocGia.Text);
                    ttl.MaTaiLieu = txtMaTaiLieu.Text;
                    ttl.NgayMuon = dtpkngaymuon.Value;
                    ttl.NgayThucTra = dtpkngayTra.Value;
                    if (txtsoluong.Text.Length != 0)
                    {
                        ttl.SoLuong = int.Parse(txtsoluong.Text);
                    }
                    if (kq == 1)
                    {
                        MessageBox.Show(this, "Loại sách này đã có", "Thong bao");
                        return;
                    }
                    trtl.ThemTra(ttl);
                    dtgvTraTL.DataSource = trtl.LaydanhsachTTL();
           // }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        //
        //private bool kiemtratontai()
        //{
        //    bool tatkt = false;
        //    string MaDocGia = txtMaDocGia.Text;
        //    string sqlconnection = "server=(local); Database=QLThuVien;Integrated security=true;";
        //    SqlConnection con = new SqlConnection(sqlconnection);
        //    SqlCommand cmd = new SqlCommand("Select * from Docgia", con);
        //    con.Open();
        //    SqlDataReader dr = cmd.ExecuteReader();
        //    while (dr.Read())
        //    {
        //        if (MaDocGia == dr.GetString(0))
        //        {
        //            tatkt = true;
        //            break;
        //        }
        //    }
        //    con.Close();
        //    return tatkt;
        //}

        // xoa tra tai lieu
        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (txtMaDocGia.Text == "")
            {
                MessageBox.Show("Ma doc gia phai co chieu dai 7 ky tu", "Thong bao", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }


            else
            {
                trtl.XoaTraTL(int.Parse(txtMaDocGia.Text));

            }
            dtgvTraTL.DataSource = trtl.LaydanhsachTTL();


        }


        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        // tìm Kiếm
        private void btntim_Click(object sender, EventArgs e)
        {
            
            


         
          
        }

        private void btnexit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnDS_Click(object sender, EventArgs e)
        {
            _repository1 = new GeneralRepository<TraTaiLieu>();
            dtgKQtimkiem.DataSource = _repository1.All().ToList();
        }

        private void btnCapnhat_Click(object sender, EventArgs e)
        {
            if (txtMaDocGia.Text == "")
            {
                MessageBox.Show("khong duoc bo trong", "loi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            else
            {

                TraTaiLieu ttl = new TraTaiLieu();
                ttl.MaDocGia = int.Parse(txtMaDocGia.Text);
                ttl.MaTaiLieu = txtMaTaiLieu.Text;
                ttl.NgayMuon = dtpkngaymuon.Value;
                ttl.NgayThucTra = dtpkngayTra.Value;
                ttl.SoLuong = int.Parse(txtsoluong.Text);
                trtl.CapNhatTra(ttl);
            }
            dtgvTraTL.DataSource = trtl.LaydanhsachTTL();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            if (txtMaDG.Text=="")
            {
                MessageBox.Show(this, "Bạn chỉ được nhập số");
            }

          else
            {
                dtgKQtimkiem.DataSource = trtl.TimTraTL(int.Parse(txtMaDG.Text));
            }
      
           
        }

        private void dtpkngayTra_ValueChanged(object sender, EventArgs e)
        {
            if (dtpkngayTra.Value <= dtpkngaymuon.Value)
            {
                MessageBox.Show("ngay tra phai sau ngay muon");
                dtpkngayTra.Value = DateTime.Now;
                return;
            }
        }

        private void txtMaDocGia_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void txtMaDG_TextChanged(object sender, EventArgs e)
        {
            
        }

    }

}



